package com.sqfw.project.wpReport.service.impl;

import com.sqfw.common.utils.StringUtils;
import com.sqfw.project.wp.domain.WpVisitInfo;
import com.sqfw.project.wp.mapper.WpVisitInfoMapper;
import com.sqfw.project.wpReport.domain.VisitQualityReport;
import com.sqfw.project.wpReport.domain.dto.VisitCountDTO;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sqfw.project.wpReport.service.IVisitQualityReportService;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 回访质量政务类报表Service业务层处理
 * 
 * @author lx
 * @date 2023-07-26
 */
@Service
public class VisitQualityReportServiceImpl implements IVisitQualityReportService 
{

    @Autowired
    private WpVisitInfoMapper wpVisitInfoMapper;

    @Override
    public List<VisitQualityReport> getVisitQualityReportList(WpVisitInfo wpVisitInfo) {

        List<String> endStates = new ArrayList<>();
        if (StringUtils.isEmpty(wpVisitInfo.getVisitType())){
            endStates.add("自办");

            endStates.add("流转");
        }else {
            endStates.add(wpVisitInfo.getVisitType());
        }


        List<VisitQualityReport> visitQualityReports = new ArrayList<>();
        for (String endState : endStates) {
            wpVisitInfo.setEndState(endState);

            // wpVisitInfo.setVisitResult("无需回访");

           // Integer visitCount = wpVisitInfoMapper.getWpVisitInfoList(wpVisitInfo);
            Integer visitCount = wpVisitInfoMapper.selectCountDistentVisitWpid(wpVisitInfo);

            VisitQualityReport visitQualityReport = new VisitQualityReport();
            visitQualityReport.setVisitType(endState);

            if (visitCount==null){
                visitQualityReport.setVisitCount(0);
            }else {
                visitQualityReport.setVisitCount(visitCount);
            }


            //满意数
            wpVisitInfo.setIssatisfied("满意");
            List<String> satisCount =  wpVisitInfoMapper.selectDistentQualityVisitWpid(wpVisitInfo);
            visitQualityReport.setSatisCount(satisCount.size());

            //不满意数
            wpVisitInfo.setIssatisfied("不满意");
            List<String> unSatisCount =  wpVisitInfoMapper.selectDistentQualityVisitWpid(wpVisitInfo);
            visitQualityReport.setUnsatisCount(unSatisCount.size());
            //一般数
            wpVisitInfo.setIssatisfied("一般");
            List<String> kindCount =  wpVisitInfoMapper.selectDistentQualityVisitWpid(wpVisitInfo);
            visitQualityReport.setKindCount(kindCount.size());
            //成功回访数
            visitQualityReport.setVisitSucessCount(satisCount.size()+unSatisCount.size()+kindCount.size());
            wpVisitInfo.setIssatisfied(null);


            visitQualityReports.add(visitQualityReport);
            wpVisitInfo.setAcceptVisit(null);
        }


        //前端要求返回集合
        return visitQualityReports;
    }
}
